package com.zjl.springbatch.job.config;

import com.zjl.springbatch.domain.Employee;
import com.zjl.springbatch.job.listener.CsvToDBJobListener;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * 将 csv文件数据 读入 临时表
 */
@Configuration
public class CsvToDBJobConfig {

    @Value("${job.data.path}")
    private String path;

    @Bean
    public FlatFileItemReader<Employee> csvToDBItemReader() throws Exception {
        FlatFileItemReader<Employee> build = new FlatFileItemReaderBuilder<Employee>()
                .name("EmployeeCsvItemReader")
                .saveState(false)
                .resource(new FileSystemResource(new File(path)))
                .delimited()
                .names("id", "name", "age", "sex")
                .targetType(Employee.class)
                .build();
        build.setEncoding("UTF-8");//设置编码格式
        return build;

    }
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Bean
    public MyBatisBatchItemWriter<Employee> csvToDBItemWriter(){
        MyBatisBatchItemWriter<Employee> itemWriter = new MyBatisBatchItemWriter<>();
        //设置数据源
        itemWriter.setSqlSessionFactory(sqlSessionFactory);

        //指定要执行的sql语句
        itemWriter.setStatementId("com.zjl.springbatch.mapper.EmployeeMapper.saveTemp");


        return itemWriter;
    }
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step csvToDBStep() throws Exception {
        return stepBuilderFactory.get("csvToDBStep")
                //每次读取数据 个数
                .<Employee, Employee>chunk(1000)
                .reader(csvToDBItemReader())//读
//                .listener(csvToDBItemReader())//处理
                .writer(csvToDBItemWriter())//写
                .taskExecutor(new SimpleAsyncTaskExecutor())//异步处理
                .build();
    }
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private CsvToDBJobListener csvToDBJobListener;
    @Bean
    public Job csvToDBJob() throws Exception {
        return jobBuilderFactory.get("csvToDBJob")
                .start(csvToDBStep())//可添加多个 Step
                .incrementer(new RunIdIncrementer())//启动多次
                .listener(csvToDBJobListener)//添加Listener  也就是job的执行前 和执行后
                .build();
    }

}
